import request from "@/utils/request";

const ROLE_BASE_URL = "/roles";

const RoleAPI = {
  getPage(queryParams?: RolePageQuery) {
    return request<any, PageResult<RolePageVO[]>>({
      url: `${ROLE_BASE_URL}/list`,
      method: "POST",
      data: queryParams,
      params: queryParams,
    });
  },

  getOptions() {
    return request<any, OptionType[]>({
      url: `${ROLE_BASE_URL}/options`,
      method: "get",
    });
  },

  getRoleOptions() {
    return request<any, OptionType[]>({
      url: `${ROLE_BASE_URL}/getAllRoleOptions`,
      method: "get",
    });
  },

  getRoleMenuIds(roleId: number) {
    return request<any, number[]>({
      url: `${ROLE_BASE_URL}/getRolePermissions/${roleId}`,
      method: "get",
    });
  },

  updateRoleMenus(data: BindPermissionVO[]) {
    return request({
      url: `${ROLE_BASE_URL}/bindPermissions`,
      method: "post",
      data: data,
    });
  },

  getFormData(id: number | undefined) {
    return request<any, RoleForm>({
      url: `${ROLE_BASE_URL}/getById/${id}`,
      method: "get",
    });
  },

  add(data: RoleForm) {
    return request({
      url: `${ROLE_BASE_URL}/create`,
      method: "post",
      data: data,
    });
  },

  update(data: RoleForm) {
    return request({
      url: `${ROLE_BASE_URL}/updateRole`,
      method: "post",
      data: data,
    });
  },

  deleteByIds(ids: string) {
    return request({
      url: `${ROLE_BASE_URL}/delete/${ids}`,
      method: "get",
    });
  },

  getAllRoles(roleId?: number) {
    return request<any, RoleFormX[]>({
      url: `${ROLE_BASE_URL}/getAllRoles/${roleId}`,
      method: "get",
    });
  },
  getVisiableRoleIds() {
    return request<any, RoleForm[]>({
      url: `${ROLE_BASE_URL}/getVisiableRoleIds`,
      method: "get",
    });
  },
  findAllRoles() {
    return request<any, RoleForm[]>({
      url: `${ROLE_BASE_URL}/getVAllRoles`,
      method: "get",
    });
  },
};

export default RoleAPI;

/** role page query */
export interface RolePageQuery extends PageQuery {
  /** keywords */
  roleName?: string;
  roleStatus?: number;
}

/** role page */
export interface RolePageVO {
  roleId?: number | string;
  roleName?: string;
  roleStatus?: number;
  remark: string;
  createAt?: Date;
  updatedAt?: Date;
}

/** role object form */
export interface RoleForm {
  roleId?: number;
  roleName?: string;
  roleStatus?: number;
  roleType?: number;
  paidStatus?: number;
  formVisiableRoleIds?: number[] | string[];
  mapRoleId?: number;
  solutionId?: number;
  userType: number;
  remark: string;
}

export interface RoleFormX {
  roleList?: RoleForm[];
  roleIdsStr: string;
}

export interface BindPermissionVO {
  push(arg0: { roleId: number; permisId: number }): unknown;
  roleId: number;
  permisId: number;
}
